/* -----------------
* brief
* 1. this is code gen by tools
* 2. mvc code are driver by the model defined with the same filename,
	 for example:
	 there is a Xmodel struct define in xmodel.go file, after the script run (lego model-mvc module_name xmodel)
     it will auto create the code under the module's path
	 m --> xmodel.go
	 v --> view
	 c --> xmodel_ctrl
* 3. you should modify this code if it doesn't satisfy your purpose
*/

package controller

import (
	"encoding/xml"
	"github.com/chanxuehong/wechat/mch/pay"
	"github.com/mjiulee/lego"
	"github.com/valyala/fasthttp"

	wxcardmch "github.com/chanxuehong/wechat/mp/card/submerchant"

	"saas_sys_base/common"
	. "saas_sys_base/modules/wechat/models"
	. "saas_sys_base/modules/wechat/service"
)

type WechatCardMchApiCtrl struct {
	wxCardMchService WxCardMchService
}

func init() {
	vc := new(WechatCardMchApiCtrl)
	_ctrlList = append(_ctrlList, vc)
}

func (vc *WechatCardMchApiCtrl) CtrlInfo() string {
	return "WechatCardMchApiCtrl"
}

func (vc *WechatCardMchApiCtrl) RoutInit() {

	lego.APIPOST("/api/mod/wechat/card/submerchant/submit", vc.CreateCardMch, true)
	//lego.APIPOST("/api/mod/wechat/card/submerchant/get", vc.GetApplyProtocol, true)
	lego.APIPOST("/api/mod/wechat/card/submerchant/batchget", vc.GetCardMchList, true)

	lego.APIPOST("/api/mod/wechat/card/getapplyprotocol", vc.GetApplyProtocol, true)

}

func (vc *WechatCardMchApiCtrl) CreateCardMch(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := "success"

	type Request struct {
		//TenantId int64 `json:"tenant_id"`
		CardMchInfoDto
	}

	type Resp struct {
		*pay.UnifiedOrderResponse
		PaySign *string
	}

	var request Request
	//var resp Resp

	for {
		sessionData := common.GetSessionData(&ctxExt)
		syswho := sessionData.SysTokenWho
		appwho := sessionData.AppTokenWho

		requestByte := ctxExt.PostBody()
		err := xml.Unmarshal(requestByte, &request)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		resp, err := vc.wxCardMchService.CreateCardMch(*appwho, syswho.TenantId, &request.CardMchInfoDto)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		response["data"] = resp
		break
	}

	response["code"] = code
	response["msg"] = msg
	//response["data"] = resp
	ctxExt.JSON(200, response)

}

func (vc *WechatCardMchApiCtrl) GetApplyProtocol(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := "success"

	type Request struct {
		//TenantId int64 `json:"tenant_id"`
		CardMchInfoDto
	}

	type Resp struct {
		*pay.UnifiedOrderResponse
		PaySign *string
	}

	var request Request
	//var resp Resp

	for {
		sessionData := common.GetSessionData(&ctxExt)
		syswho := sessionData.SysTokenWho
		appwho := sessionData.AppTokenWho

		requestByte := ctxExt.PostBody()
		err := xml.Unmarshal(requestByte, &request)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		_, err = vc.wxCardMchService.GetApplyProtocol(*appwho, syswho.TenantId, &request.CardMchInfoDto)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		//response["data"] = resp
		break
	}

	response["code"] = code
	response["msg"] = msg
	//response["data"] = resp
	ctxExt.JSON(200, response)

}

func (vc *WechatCardMchApiCtrl) GetCardMchList(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := "success"

	type Request struct {
		//TenantId int64 `json:"tenant_id"`
		wxcardmch.CardMchBatchGetPara
	}

	type Resp struct {
		*wxcardmch.CardMchBatchGetResp
	}

	var request Request
	var resp Resp

	for {
		sessionData := common.GetSessionData(&ctxExt)
		//syswho := sessionData.SysTokenWho
		appwho := sessionData.AppTokenWho

		requestByte := ctxExt.PostBody()
		err := xml.Unmarshal(requestByte, &request)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		resp.CardMchBatchGetResp, err = vc.wxCardMchService.GetCardMchList(*appwho, &request.CardMchBatchGetPara)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		response["data"] = resp
		break
	}

	response["code"] = code
	response["msg"] = msg
	//response["data"] = resp
	ctxExt.JSON(200, response)

}
